package com.youdoneed.user.application;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.youdoneed.base.application.AppException;
import com.youdoneed.base.common.OkHttpUtils;

/**
 * 短信验证码和短信通知发送接口。
 *
 * @author Ewing
 * @date 2017/6/1
 */
public class PhoneMessage {
    /**
     * 公共参数
     */
    private static final String url = "http://gw.api.taobao.com/router/rest";
    private static final String appkey = "24597263";
    private static final String secret = "c65c000dc0a36b9cd218e0736ce31775";
    private static final TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);

    /**
     * 通用短信发送接口。
     *
     * @param phoneNo      电话号码
     * @param templateCode 模板编码
     * @param jsonParam    模板参数
     * @return 是否发送成功
     */
    public static AlibabaAliqinFcSmsNumSendResponse sendSMSG(String phoneNo, String templateCode, String jsonParam) {
        try {
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            //短信类型
            req.setSmsType("normal");
            //短信签名
            req.setSmsFreeSignName("Coolface酷脸");
            //短信验证码模板变量
            req.setSmsParamString(jsonParam);
            //短信接收号码
            req.setRecNum(phoneNo);
            //短信模板ID（短信通知）
            req.setSmsTemplateCode(templateCode);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

            return rsp;
        } catch (Exception e) {
            throw new AppException("短信验证码发送失败！");
        }
    }

    /**
     * 短信验证码发送
     *
     * @param phoneNo 电话号码
     * @param code    验证码
     * @return 是否发送成功
     */
    public static AlibabaAliqinFcSmsNumSendResponse sendCheckCode(String phoneNo, String code) {
        String templateCode = "SMS_50980123";
        //短信通知模板的内容
        String jsonParam = "{\"code\":\"" + code + "\"}";
        return sendSMSG(phoneNo, templateCode, jsonParam);
    }

    /**
     * 国际短信发送API参数
     */
    private static final String urlNational = "http://api2.santo.cc/submit";  //应用地址 (无特殊情况时无需修改)
    private static final String cpid = "2yuzp5";  //应用id (无特殊情况时无需修改)
    private static final String cppwd = "dFWcwCbw";  //应用密钥 (无特殊情况时无需修改)
    private static final String command = "MT_REQUEST";        // MT_REQUEST：短信  VO_REQUEST：语音
    private static final String sa = "CoolFace";        // //自定义发送者号码 (仅限最多16位数字或者11位字母)

    /**
     * 国际短信验证码发送
     *
     * @param phoneNo 电话号码
     * @param code    验证码
     * @return 是否发送成功
     */
    public static String sendNationalCode(String phoneNo, String code) {
        String content = "Coolface: Your verification code is " + code + ", valid for ten minutes," +
                " please do not tell other people, if not you do, please ignore the message.";
        String result = OkHttpUtils.formPost(urlNational)
                .param("command", command).param("cpid", cpid)
                .param("cppwd", cppwd).param("sa", sa)
                .param("da", phoneNo).param("sm", content)
                .callForString();
        return result;
    }

}
